package Aleehot100;

import java.util.HashSet;

/**
 * ClassName: c03
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/20 22:22
 * @Version 1.0
 * @Since 1.0
 */
public class c03 {
    public static void main(String[] args) {
        int[] arr = {0,3,7,2,5,8,4,6,0,1};
        System.out.println(longestConsecutive(arr));
    }

    static HashSet<Integer> set = new HashSet<>();

    public static int longestConsecutive(int[] nums) {
        set.clear();
        // 加入元素
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        int longestRes = 0, curnum, curcount;
        // 统计元素, 这里其实是可以画图的，就是一个单调性的问题
        for (Integer i : set) {
            if (!set.contains(i + 1)) {
                curnum = i;
                curcount = 1;
                while (set.contains(curnum - 1)) {
                    curnum--;
                    curcount ++;
                }
                longestRes = Math.max(longestRes, curcount);
            }
        }
        return longestRes;
    }
}
